package cn.myframe.util;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: ynz
 * @Date: 2019/3/20/020 8:48
 * @Version 1.0
 */
public class StockQueue<T> {

    private Object[] array;

    private int length;

    private int index=0;

    public StockQueue(int num){
        length = num;
        array = new Object[num];
    }

    /**
     * 添加数据
     * @param e
     */
    public void push(T e) {
        array[index] = e;
        index = (index+1)%length;
    }

    public Object[] getArray(){
        return array;
    }

    public  <T> T[] convertArray(Class<T> targetType) {
        int index = array.length;
        for (int i = 0; i < array.length; i++) {
            if(array[i] == null){
                index = i;
                break;
            }
        }
        if (targetType == null  ) {
            return (T[]) array;
        }
        T[] targetArray = (T[]) Array.newInstance(targetType, index);
        try {
            System.arraycopy(array, 0, targetArray, 0, index);
        } catch (ArrayStoreException e) {
            e.printStackTrace();
        }
        return targetArray;
    }







    public static void main(String[] args) {
      //
        List<String> list = new ArrayList<String>();
        StockQueue<Integer> stockQueue = new StockQueue<>(10);
        for (int i = 0; i < 100; i++) {
            stockQueue.push(i);
        }

        for (Object o:stockQueue.getArray()){
            System.out.println(o);
        }


    }
}
